﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.ObjectModel;
using University.Workshop.Model;
using University.Workshop.DataAccess;
using University.Workshop.Business;
using System.IO;

namespace University.Workshop.WebApp
{
    public partial class UserProfile : System.Web.UI.Page
    {
        private Collection<Product> products;
        private string relatedUser;
        private const int MAX = 5;
        protected void Page_Load(object sender, EventArgs e)
        {
            relatedUser = Request["user"];
            if (relatedUser == null)
                MultiViewProfile.ActiveViewIndex = 1;
            else
            {
                try
                {
                    User requestedUser = UserDataAccess.GetUserByEmail(relatedUser);
                    if (requestedUser != null)
                    {
                        MultiViewProfile.ActiveViewIndex = 0;
                        userName.InnerHtml = relatedUser;
                        GetData(requestedUser);
                        SetControls();
                        LoadImage(requestedUser.Id);
                        lbseller.Text = TbFirstName.Text + "'s feedback :";
                        nstars.Text =
                            RatingDataAccess.GetUserRatingsSum(requestedUser.Id).ToString(CultureInfo.CurrentCulture);
                        feedback.Text =
                            RatingDataAccess.GetLastUserFeedback(requestedUser.Id);
                    }
                    else
                    {
                        MultiViewProfile.ActiveViewIndex = 1;
                    }
                }
                catch (Exception exc)
                {
                    Response.Write("<script>alert('" + exc.Message + "'); history.back();</script>");
                }
            }
        }

        private void SetControls()
        {
            try
            {
                PrivacySetting privacySetting;
                if (relatedUser != User.Identity.Name)
                {
                    privacySetting = PrivacySettingsManager.GetPrivacySettingByEmail(relatedUser);
                }
                else
                {
                    privacySetting = new PrivacySetting { Setting = "00000000"+PrivacySettingsManager.GetPrivacySettingByEmail(relatedUser).Setting[8]};
                }
                int cont = 0;
                if (privacySetting != null)
                {
                    if (privacySetting.Setting[0] == '1')
                    {
                        rowFirstName.Visible = false;
                        LbFirstName.Visible = false;
                        TbFirstName.Visible = false;
                        cont++;
                    }
                    if (privacySetting.Setting[1] == '1')
                    {
                        rowLastName.Visible = false;
                        LbLastName.Visible = false;
                        TbLastName.Visible = false;
                        cont++;
                    }
                    if (privacySetting.Setting[2] == '1')
                    {
                        rowGender.Visible = false;
                        LbGender.Visible = false;
                        TbGender.Visible = false;
                        cont++;
                    }
                    if (privacySetting.Setting[3] == '1')
                    {
                        rowBirthday.Visible = false;
                        LbBirthday.Visible = false;
                        TbBirthday.Visible = false;
                        cont++;
                    }
                    if (cont == 4)
                        MultiViewPersonalInfo.ActiveViewIndex = 1;
                    cont = 0;
                    if (privacySetting.Setting[4] == '1')
                    {
                        rowPhone.Visible = false;
                        LbPhone.Visible = false;
                        TbPhone.Visible = false;
                        cont++;
                    }
                    if (privacySetting.Setting[5] == '1')
                    {
                        rowMobile.Visible = false;
                        LbMobile.Visible = false;
                        TbMobile.Visible = false;
                        cont++;
                    }
                    if (privacySetting.Setting[6] == '1')
                    {
                        rowDepartment.Visible = false;
                        LbDepartment.Visible = false;
                        TbDepartment.Visible = false;
                        cont++;
                    }
                    if (privacySetting.Setting[7] == '1')
                    {
                        rowCity.Visible = false;
                        LbCity.Visible = false;
                        TbCity.Visible = false;
                        cont++;
                    }
                    if (cont == 4)
                        MultiViewContactInfo.ActiveViewIndex = 1;
                }
                LblInformation.Visible = false;
                if (products != null && privacySetting != null)
                {
                    int count = (privacySetting.Setting[8] - 48);
                    if(count==0)
                        LblInformation.Visible = true;
                    else LblInformation.Visible = false;
                    dataList.DataSource = products;
                    dataList.DataBind();
                }
                if (products == null)
                    LblInformation.Visible = true;
                else
                {
                    if(products.Count==0)
                        LblInformation.Visible = true;
                }
            }
            catch (Exception exc)
            {
                Response.Write("<script>alert('" + exc.Message + "'); history.back();</script>");
            }
        }

        private void GetData(User user)
        {
            TbFirstName.Text = user.PersonalInformation.FirstName;
            TbLastName.Text = user.PersonalInformation.LastName;
            TbGender.Text = (user.PersonalInformation.Gender == 'M' || user.PersonalInformation.Gender == 'm') ? "Male" : "Female";
            TbBirthday.Text = user.PersonalInformation.Birthday.ToShortDateString();
            TbPhone.Text = user.ContactInformation.Phone.ToString();
            TbMobile.Text = user.ContactInformation.MobilePhone.ToString();
            TbDepartment.Text = user.ContactInformation.Department;
            TbCity.Text = user.ContactInformation.City;
            products = PrivacySettingsManager.GetShownProducts(user.Id);
        }

        public void LoadImage(long userId)
        {

            if (Directory.Exists(Server.MapPath("~/Thumbnails/U") + userId + "/"))
            {
                DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/Thumbnails/U") + userId + "/");
                FileInfo[] currentImages = dir.GetFiles();
                string thumbUrl = "~/Thumbnails/U" + userId + "/";
                foreach (FileInfo fi in currentImages)
                {
                    if (String.IsNullOrEmpty(image1.ImageUrl))
                    {
                        image1.ImageUrl = thumbUrl + fi.Name;

                    }
                }
            }
            else
            {
                image1.ImageUrl = "~/Thumbnails/NoImage/noimage.jpg";
            }
        }
    }
}